package com.foo.HistoricalDataTest;

import java.util.Calendar;
import java.util.Date;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;

import junit.framework.Assert;

import org.junit.Test;

import com.foo.HistoricalData.HistoricDataProviderRemote;
import com.foo.ReadData.DataReceiver;


public class HistoricalDataTest {

	@Test
	public void historicalDataTestPositive()
	{
		HistoricDataProviderRemote dataprovider = null;
		try
		{
			Properties properties = new Properties();
			properties.put(Context.PROVIDER_URL, "jnp://localhost:1099");
			properties.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
			properties.put(Context.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");
			Context ctx = new InitialContext(properties);
			Object ref = ctx.lookup("HistoricDataProvider/remote");
			//DataProviderRemote 
			dataprovider = (HistoricDataProviderRemote) PortableRemoteObject.narrow(ref, HistoricDataProviderRemote.class);

			String ticker = "A";

			Calendar calendar = Calendar.getInstance();
			calendar.set(2009, 9, 10);
			Date startDate = calendar.getTime();
			calendar.set(2009, 9, 15);
			Date endDate = calendar.getTime();

			System.out.println("Ticker: " + ticker + ", Start: " + startDate + ", End: " + endDate);

			DataReceiver recieve = new DataReceiver();
			dataprovider.sendHistoricData(ticker, startDate, endDate);

			String[] test=recieve.readtickerinfodata();
			Assert.assertTrue(test.length != 1);
		}
		catch(Exception e)
		{
			e.printStackTrace();
			Assert.fail();
		}
	}

	@Test
	public void historicalDataTestNegative()
	{
		HistoricDataProviderRemote dataprovider = null;
		try
		{
			Properties properties = new Properties();
			properties.put(Context.PROVIDER_URL, "jnp://localhost:1099");
			properties.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
			properties.put(Context.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");
			Context ctx = new InitialContext(properties);
			Object ref = ctx.lookup("HistoricDataProvider/remote");
			//DataProviderRemote 
			dataprovider = (HistoricDataProviderRemote) PortableRemoteObject.narrow(ref, HistoricDataProviderRemote.class);

			String ticker = "A";

			Calendar calendar = Calendar.getInstance();
			calendar.set(2011, 9, 10);
			Date startDate = calendar.getTime();
			calendar.set(2011, 9, 15);
			Date endDate = calendar.getTime();

			System.out.println("Ticker: " + ticker + ", Start: " + startDate + ", End: " + endDate);

			DataReceiver recieve = new DataReceiver();
			dataprovider.sendHistoricData(ticker, startDate, endDate);

			String[] test=recieve.readtickerinfodata();
			Assert.assertTrue(test.length == 1);
		}
		catch(Exception e)
		{
			e.printStackTrace();
			Assert.fail();
		}
	}
}



